var imgReady = (function() {

    var list = [],
        intervalId = null,

        // 用来执行队列
        tick = function() {
            var i = 0;
            for (; i < list.length; i++) {
                list[i].end ? list.splice(i--, 1) : list[i]();
            };
            !list.length && stop();
        },

        // 停止所有定时器队列
        stop = function() {
            clearInterval(intervalId);
            intervalId = null;
        };

    return function(url, ready, load, error) {

        var onready, width, height, newWidth, newHeight,
            img = new Image();

        img.src = url;

        // 如果图片被缓存，则直接返回缓存数据
        if (img.complete) {

            ready.call(img);
            load && load.call(img);
            return;
        };

        width = img.width;
        height = img.height;

        // 加载错误后的事件
        img.onerror = function() {
            error && error.call(img);

            onready.end = true;
            img = img.onload = img.onerror = null;
        };

        // 图片尺寸就绪
        onready = function() {
            
            newWidth = img.width;
            newHeight = img.height;
            if (newWidth !== width || newHeight !== height ||
                // 如果图片已经在其他地方加载可使用面积检测

                newWidth * newHeight > 1024
            ) {

                ready.call(img);
                onready.end = true;
            };
        };
        onready();

        // 完全加载完毕的事件
        img.onload = function() {
            // onload在定时器时间差范围内可能比onready快
            // 这里进行检查并保证onready优先执行
            !onready.end && onready();

            load && load.call(img);

            // IE gif动画会循环执行onload，置空onload即可
            img = img.onload = img.onerror = null;
        };

        // 加入队列中定期执行
        if (!onready.end) {
            list.push(onready);
            // 无论何时只允许出现一个定时器，减少浏览器性能损耗
            if (intervalId === null) intervalId = setInterval(tick, 40);
        };
    };
})();

(function($) {
    $(function() {
        $hasPicElem = $(".upload-haspic");
        $.each($hasPicElem, function(index, el) {
            var $el = $(el);
            var src = $el.attr("data-src");
            if (!!src) {
                imgReady(src, function() {
                    $el.attr({ "data-w": this.width, "data-h": this.height })
                });
            }
        });
    })
})(Zepto);

$(function() {
    var uploader1 = $(".J_pickfiles1").plupload({
        url: './php/upload.php',
        //动态添加<input type="hidden"> 表单提交的数据的时候用到
        inputHidden: false,
        filters: {
            max_file_count: 1,
            max_file_size: '5mb',
        },
        //传递参数 uid 等等
        multipart_params: {
            uid: 10
        },
        //初始化调用函数
        initLoad: function(up) {

        },
        //每个文件上传后触发函数(上传成功或者失败都可以触发)
        fileUploaded: function(up, file, responseInfo) {

            var response = $.parseJSON(responseInfo.response);
            var $parent = up.$container.parent();
            console.dir($parent)
            if ($parent.hasClass('upload-haspic')) {
                console.dir($parent.children('.pic'))
                $parent.children('.pic').hide();
                $parent.find('.reupload').hide();
                $parent.children('.tip').hide();
            }
            if (!!response.OK && response.OK == 1) {
                file.$success.show();
                file.$progress.hide().children().css("width", 0);
                //设置input的值
                !!file.$inputHidden.length && file.$inputHidden.val("");
            } else {
                //服务器上传失败
                file.status = plupload.FAILED;
                file.$error.show();
                file.$progress.hide().children().css("width", 0);
            }

        },
        fileRemoved: function(up, file) {
            var $parent = up.$container.parent();
            if ($parent.hasClass('upload-haspic')) {

                $parent.children('.pic').show();
                $parent.find('.reupload').show();
                $parent.children('.tip').show();
            }
        },
        //plupload 执行全部的上传事件后触发该函数，只触发一次
        uploadComplete: function(up, files) {

        }

    });


    var uploader2 = $(".J_pickfiles2").plupload({
        url: './php/upload.php',
        //动态添加<input type="hidden"> 表单提交的数据的时候用到
        inputHidden: false,
        filters: {
            max_file_count: 1,
            max_file_size: '5mb',
        },
        //传递参数 uid 等等
        multipart_params: {
            uid: 10
        },
        //初始化调用函数
        initLoad: function(up) {

        },
        //每个文件上传后触发函数(上传成功或者失败都可以触发)
        fileUploaded: function(up, file, responseInfo) {

            var response = $.parseJSON(responseInfo.response);
            var $parent = up.$container.parent();
            console.dir($parent)
            if ($parent.hasClass('upload-haspic')) {
                console.dir($parent.children('.pic'))
                $parent.children('.pic').hide();
                $parent.find('.reupload').hide();
                $parent.children('.tip').hide();
            }
            if (!!response.OK && response.OK == 1) {
                file.$success.show();
                file.$progress.hide().children().css("width", 0);
                //设置input的值
                !!file.$inputHidden.length && file.$inputHidden.val("");
            } else {
                //服务器上传失败
                file.status = plupload.FAILED;
                file.$error.show();
                file.$progress.hide().children().css("width", 0);
            }

        },
        fileRemoved: function(up, file) {
            var $parent = up.$container.parent();
            if ($parent.hasClass('upload-haspic')) {

                $parent.children('.pic').show();
                $parent.find('.reupload').show();
                $parent.children('.tip').show();
            }
        },
        //plupload 执行全部的上传事件后触发该函数，只触发一次
        uploadComplete: function(up, files) {

        }

    });


    var uploader3 = $("#J_pickfiles3").plupload({
        url: './php/upload.php',
        //动态添加<input type="hidden"> 表单提交的数据的时候用到
        inputHidden: false,
        filters: {
            max_file_count: 1,
            max_file_size: '5mb',
        },
        //传递参数 uid 等等
        multipart_params: {
            uid: 10
        },
        //初始化调用函数
        initLoad: function(up) {

        },
        //每个文件上传后触发函数(上传成功或者失败都可以触发)
        fileUploaded: function(up, file, responseInfo) {

            var response = $.parseJSON(responseInfo.response);
            var $parent = up.$container.parent();
            console.dir($parent)
            if ($parent.hasClass('upload-haspic')) {
                console.dir($parent.children('.pic'))
                $parent.children('.pic').hide();
                $parent.find('.reupload').hide();
                $parent.children('.tip').hide();
            }
            if (!!response.OK && response.OK == 1) {
                file.$success.show();
                file.$progress.hide().children().css("width", 0);
                //设置input的值
                !!file.$inputHidden.length && file.$inputHidden.val("");
            } else {
                //服务器上传失败
                file.status = plupload.FAILED;
                file.$error.show();
                file.$progress.hide().children().css("width", 0);
            }

        },
        fileRemoved: function(up, file) {
            var $parent = up.$container.parent();
            if ($parent.hasClass('upload-haspic')) {

                $parent.children('.pic').show();
                $parent.find('.reupload').show();
                $parent.children('.tip').show();
            }
        },
        //plupload 执行全部的上传事件后触发该函数，只触发一次
        uploadComplete: function(up, files) {

        }

    });
});



(function($) {
    var openPhotoSwipe = function(items) {
        var pswpElement = document.querySelectorAll('.pswp')[0],
            options = {
                index: 0,
                history: false,
                focus: false,
                showAnimationDuration: 0,
                hideAnimationDuration: 0
            };

        var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
    };
    $(function() {
        var $itemElems = $(".page-upload-gczl .case-tip");

        $.each($itemElems, function(index, el) {
            var $el = $(el);
            $el.attr("data-pswp-uid", "case-tip-" + index);
            $el.on("click", function() {
                var item = [{}];
                var $this = $(this);
                item[0]["src"] = $this.attr("data-src");
                item[0]["w"] = $this.attr("data-w");
                item[0]["h"] = $this.attr("data-h");
                console.dir(item);
                openPhotoSwipe(item);
            })
        });
    })
})(Zepto);






(function($) {
    var openPhotoSwipe = function(items) {
        var pswpElement = document.querySelectorAll('.pswp')[0],
            options = {
                index: 0,
                history: false,
                focus: false,
                showAnimationDuration: 0,
                hideAnimationDuration: 0
            };
        var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
    };

    var showHandle=function($el){
         var item = [{}];
            item[0]["src"] =$el.attr("data-src");
            item[0]["w"] = $el.attr("data-w");
            item[0]["h"] = $el.attr("data-h");
            openPhotoSwipe(item);
    }

    $(function() {


        $(".upload-haspic.auditing").on("click", function() {
            
            showHandle($(this))
        });

        

        $(".upload-haspic.success").on("click",function(){
             showHandle($(this))
        })
        $(document).on("click", ".coms-upload-list .preview-pic", function() {
             showHandle($(this))
        })
    })

})(Zepto);